home *** CD-ROM | disk | FTP | other *** search
- Path: hobbes.sco.COM!md
- From: md@sco.COM (Michael Davidson)
- Newsgroups: comp.lang.c
- Subject: Re: help with strcmp
- Date: 2 Apr 1996 21:21:43 GMT
- Organization: The Santa Cruz Operation, Inc.
- Message-ID: <4js5p7$q44@hobbes.sco.COM>
- References: <4jpiek$lp6@blaze.cs.jhu.edu>
- NNTP-Posting-Host: execsrvr.research.sco.com
- Cc:
-
-
- In article <4jpiek$lp6@blaze.cs.jhu.edu>,
- John E. Davis <lasher@hops.cs.jhu.edu> wrote:
- >For some reason when I try to compare the strings in the following
- >snippet I consistently get a core dump (running on a UNIX machine running
- >Solaris). Could anyone point out what may be going wrong? I have run it
- >through the debugger and that was no help at all for me. here is the
- >code snippet:
-
- Ignoring the fact that you have declared main() incorrectly, and
- that you are not checking either argc (to make sure that argv[1]
- is legal) or the return value from the first fopen(), your problem
- appears to be that you are not checking for end-of-file correctly.
-
- More specifically you test for end of file at the top of your
- loop, but *not* immediately after the call to fgets().
-
- feof() can't predict the future - if tells you the *current*
- EOF status of the file, not what it *will* be after the next
- time you try to read the file (eg by calling fgets()).
-
- Consider the limiting case of a 0 length file - immediately after
- the fopen() feof() will return FALSE, however the first call to
- fgets() will return NULL and subsequent calls to feof() will
- return TRUE. Your code, however will try to use the NULL pointer
- returned by the call to fgets() that actually hit end-of-file.
-
-
- You could fix this by rewriting your loop thus:
-
- while (fp = fgets(data, 40, handle)) {
- n = 0;
-
- if ( strcasecmp(fp, "<action>\n") == 0)
- parseAction(handle, dest);
-
- ...
- }
-
-